package cn.edu.cqvie.atc.sort.impl;

import cn.edu.cqvie.atc.sort.Sort;

import java.util.Comparator;

/**
 * 选择排序 << 简单排序 时间复杂度 O(n2)  >>
 *
 * @author Zheng Shaohong
 */
public class SelectionSort<T> extends BaseSort<T> {

    public static void main(String[] args) {

        Sort<Integer> sort = new SelectionSort<>();
        Integer[] arr = new Integer[]{1, 3, 4, 5, 10, 9};
        sort.display(arr);
        sort.sort(arr, new IntegerComparator());
        sort.display(arr);
    }

    @Override
    public void sort(T[] arr, Comparator<T> c) {
        int k = 0;
        T tmp = null;
        for (int i = 0; i < arr.length; i++) {
            k = i;
            for (int j = i + 1; j < arr.length; j++) {
                if (c.compare(arr[j], arr[k]) < 0) {
                    k = j;
                }
            }
            tmp = arr[i];
            arr[i] = arr[k];
            arr[k] = tmp;
        }
    }
}
